#!/usr/bin/env python
#
#
#
#
#

"""
#TODO: documentation!
"""

__author__ = "$Author: dlacewell $"
__version__ = "$Revision: 13 $"
__date__ = "$Date: 2007-09-03 13:37:50 -0500 (Mon, 03 Sep 2007) $"
__license__ = "see readme.txt"
__copyright__ = "DR0ID (c) 2006-2007"
__url__ = "http://dr0id.ch.vu"
__email__ = "dr0id@bluewin.ch"

from bipy.components import Component

class Signal(object):
    
    def __init__(self):
        self._listeners = []
        
    def register(self, listener, prio=None):
        if listener in self._listeners:
            self.unregister(listener)
        if prio and prio < len(self._listeners):
            self._listeners.insert(prio, listener)
        else:
            self._listeners.append(listener)
        
    def unregister(self, listener):
        if listener in self._listeners:
            self._listeners.remove(listener)
            
    def emit(self, *args, **kwargs):
        [listener(*args, **kwargs) for listener in self._listeners]



class EventManager(Component):
    """Class Eventsystem
    """
    def __init__(self):
        super(EventManager, self).__init__('events', 10)
        
        # internal data
        self.signals = {}
        self.events = []
        # Signal handlers
        #   pygame events
        self.signals['quit'] = Signal()
        self.signals['render'] = Signal()
    
    def emit(self, event):
        self.events.append(event)
        
    def dispatch(self):
        for event in self.events:
            #todo handle events
            pass
        
        self.signals['render'].emit()
        
        self.events = []

component_class = EventManager

